package aom.ju.ss

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import aom.ju.ss.databinding.Main2Binding
import java.math.BigDecimal

class MainActivity : BaseActivity2<Main2Binding>() {

    var mon = 0
    var nlv = 0.0
    var nx = 0

    var nck = 0


    var all = 0
    var alllx = 0
    var bj = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.js.setOnClickListener {
            all = 0
            alllx = 0
            if (binding.ck.text.isEmpty()) {
                show("输入每月存款", this)
                binding.ck.error = "输入每月存款"
                return@setOnClickListener
            }
            mon = binding.ck.text.toString().toInt()
            if (binding.nlv.text.isEmpty()) {
                show("输入年利率", this)
                binding.nlv.error = "输入年利率"
                return@setOnClickListener
            }
            nlv = binding.nlv.text.toString().toDouble()
            if (binding.nx.text.isEmpty()) {
                show("输入存款年限", this)
                binding.nx.error = "输入存款年限"
                return@setOnClickListener
            }
            nx = binding.nx.text.toString().toInt()

            bj = mon * nx * 12
            nck = mon * 12

            for (a in 1..nx) {
                Log.e("TAG", "onCreate: " + a)
                if (a == 1) {
                    all += nck + (nck * (nlv / 100)).toInt()
                } else {
                    all = (all + nck) + ((all + nck) * (nlv / 100)).toInt()
                }


                Log.e("TAG", "onCreate11: " + all)

            }
            alllx = all - bj

            Log.e("TAG", "onCreate: "+ BigDecimal(0.1.toString())*BigDecimal(0.1.toString()))
            Log.e("TAG", "onCreate: "+ 0.1)
            Log.e("TAG", "onCreate: "+BigDecimal(0.1))
            binding.tv.text = "本金：" + bj.toString().gsh
            binding.tv.text = binding.tv.text.toString() + "\n总额：" + all.toString().gsh
            binding.tv.text = binding.tv.text.toString() + "\n总利息：" + alllx.toString().gsh
        }

    }


}


val String.gsh: String
    get() {
        var w = 0
        var wc = this.toInt()
        if (this.toInt() > 10000) {
            w = this.toInt() / 10000
            wc = this.toInt() % 10000
        }

        return w.toString() + "万" + if (wc == 0) "" else wc.toString()
    }

private var toast: Toast? = null

fun show(info: Any?, co: Context) {
    if (info.toString() == "重复请求") {
        return
    }
    if (info.toString().isEmpty() || info.toString().length > 100 || info.toString()
            .contains("code")
    ) {
        return
    }
    if (toast == null) {
        toast = Toast.makeText(co, null, Toast.LENGTH_SHORT)
    }
    try {
        toast!!.setText(info.toString() + "")
        toast!!.show()
    } catch (c: Exception) {

        c.message?.let { Log.e("", it) }
    }
}